package com.jxdinfo.hussar.activitidemo.utils.log.thread;

import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * 
 * 数据库连接属性文件读取操作
 * 
 */
public final class LogWriteProperties {
	private static final Logger log = Logger.getLogger(LogWriteProperties.class);
	
	private static String configFile = "logWriteConfig.properties";
	private static Properties props = null;
	private static LogWriteProperties instance = null;

	private LogWriteProperties() {
	}

	/**
	 * 单例模式
	 * 
	 * @return
	 */
	public static LogWriteProperties singlton() {
		if (instance == null) {
			instance = new LogWriteProperties();
		}
		return instance;
	}


	/**
	 * 重新加载JDBC配置文件
	 * 
	 */
	public void reloadJdbcProperties() {
		loadJdbcProperties();
	}

	/**
	 * 根据JDBC属性名称获得属性值
	 * 
	 * @param propertyName
	 * @return
	 */
	public String getProperty(String propertyName) {

		if (props == null) {
			loadJdbcProperties();
		}
		try {
			return props.getProperty(propertyName);
		} catch (Exception e) {
			log.error("get property value by " + propertyName
					+ " property name error:" + e.getMessage());
			return null;
		}

	}

	/**
	 * 从JDBC文件加载Properties
	 * 
	 */
	private void loadJdbcProperties() {
		try {
			log.debug("load config property file.");
			InputStream in = LogWriteProperties.class.getClassLoader()
					.getResourceAsStream(configFile);
			props = new Properties();
			props.load(in);
		} catch (Exception e) {
			log.error("load config property file error:" + e.getMessage());
		}
	}
}